/**
 * Created by Administrator on 2018/10/22.
 */

$(function(){

    /* 个人关注 */
    $(".information-followA").on('click',function(){
    	var that = $(this);
		var authorid = that.data('authorid');
		var userid = $("#userid").data("userid");
		if(userid != ''){
			 if(that.hasClass('information-Active') || that.hasClass('information-iMutual')){
				 $.ajax({ 
					 url: '/operate/'+ userid + '/cancel/' + authorid, 
					 type: "POST",
			         data: {},
			         dataType: "json",//请求数据类型
					 success: function(data){
						 if(data.code == 0){
					        that.removeClass("information-Active");
					        that.removeClass("information-iMutual");
					        that.text("+关注")
					        var num = Number($('#F' + authorid).text());
					        $('#F' + authorid).text(num-1);
						 }else{
							 layer.msg(data.msg);
						 }
					 },
					 error : function() {
						 layer.msg("取消关注异常");
					 }
				 })	
			}else{
				$.ajax({ 
					 url: '/operate/'+ userid + '/follow/' + authorid, 
					 type: "POST",
			         data: {},
			         dataType: "json",//请求数据类型
					 success: function(data){
						 var result = data.data;
						 if(data.code == 0){
							if(result == 1){
								that.addClass("information-Active");
						        that.text("✓已关注")
							 }else if(result == 2){
								 that.addClass("information-iMutual");
							     that.text("⇋互为关注")
							 }
							 var num = Number($('#F' + authorid).text());
						     $('#F' + authorid).text(num+1);
						 }else{
							 layer.msg(data.msg);
						 }
					 },
					 error : function() {
						 layer.msg("关注失败");
					 }
				 })
			}
			
		}else{
			window.location.href = "/signup"
		}
    });


    var page = 2;
	$(function () {
		//加载更多
		$(".LoadMoreA").click(function() {
			var that = $(this);
			var type = that.data('type');
			var scope = that.data('scope');
			var url = "";
			if(type == 'diary'){
				url = '/load/loadUserDiaryList';
			}else if(type == 'like'){
				url = '/load/loadLikeDiaryList';
			}else if(type == 'follow'){
				url = '/load/loadFollowList';
			}else if(type == 'fans'){
				url = '/load/loadFansList';
			}
			$.ajax({ 
				 url: url, 
				 type: "GET",
		         data: {page: page,scope: scope},
		         dataType: "json",//请求数据类型
				 success: function(data){
					 if(data.code == 0){
						 if(data.count > 0){
							 var result = data.data;
							 $("#userdiarylist").append(result);
							 page++;
							 $('.wall').jaliswall({item: '.article'});
							 if(data.count < 20){
								 $('.LoadMore').empty();
								 $('.LoadMore').text('没有更多数据了');
							 }
						 }else{
							 $('.LoadMore').empty();
							 $('.LoadMore').text('没有更多数据了');
						 }
					 }else{
						 $('.LoadMore').empty();
						 $('.LoadMore').text(data.msg);
					 }
				 },
				 error : function() {
					 $('.LoadMore').empty();
					 $('.LoadMore').text('加载失败');
				 }
			 })
		});
	})

    /* 瀑布流 */
    $('.wall').jaliswall({item: '.article'});

    

    /* 粉丝关注列表 */
    $(".information-switch2").on('click',function(){
        var that = $(this);
        if( that.hasClass('information-noSwitch') ){
            that.removeClass("information-noSwitch");
            that.text("+关注")
        }else {
            that.addClass("information-noSwitch");
            that.text("已关注")
        }
    })

})

//用户取消喜欢
function nulike(that){
	var _that = $(that);
    layer.open({
        content: '确定取消此喜欢吗?',
        btn: ['确定', '取消'],
        yes: function (index) {
            $.post("/operate/like/" + _that.data('id'), {},function(result){
    			if(result.code == 0){
    				layer.close(index);
    				layer.msg('取消成功',{icon:1});
    				_that.parents('.inform-diaryTypeLi').remove();
    				var num = Number($('#userVoteNumber').text());
				    $('#userVoteNumber').text(num-1);
    				$('.wall').jaliswall({item: '.article'});
    			}else{
    				layer.close(index);
    				layer.msg(result.msg);
    			}
    		});
        },
        no: function () {

        }
    });
}

//用户点击喜欢
function userlike(that){
	var _that = $(that);
	if(_that.hasClass('inform-diaryTypeLi-liActive')){
		layer.open({
	        content: '确定取消此喜欢吗?',
	        btn: ['确定', '取消'],
	        yes: function (index) {
	            $.post("/operate/like/" + _that.data('id'), {},function(result){
	    			if(result.code == 0){
	    				layer.close(index);
	    				_that.removeClass('inform-diaryTypeLi-liActive');
	    				layer.msg('取消成功',{icon:1});
	    				$('.wall').jaliswall({item: '.article'});
	    			}else{
	    				layer.close(index);
	    				layer.msg(result.msg);
	    			}
	    		});
	        },
	        no: function () {

	        }
	    });
	}else{
		$.post("/operate/like/" + _that.data('id'), {},function(result){
			if(result.code == 0){
				 _that.addClass('inform-diaryTypeLi-liActive');
				 $('.wall').jaliswall({item: '.article'});
			}else{
				layer.msg(result.msg);
			}
		});
	}
	
	
}

//取消关注
function follow(userid,authorid,thzs){
	 var that = $(thzs);
	 layer.open({
	        content: '是否取消关注?',
	        btn: ['确定', '取消'],
	        yes: function (index) {
	        	$.ajax({ 
	       		 url: '/operate/'+ userid + '/cancel/' + authorid, 
	       		 type: "POST",
	                data: {},
	                dataType: "json",//请求数据类型
	       		 success: function(data){
	       			 if(data.code == 0){
	       		         window.location.reload();
	       			 }else{
	       				 layer.msg(data.msg,{icon:7});
	       			 }
	       		 },
	       		 error : function() {
	       			 layer.msg("取消关注异常",{icon:7});
	       		 }
	       	 })
	        },
	        no: function () {

	        }
	    });
}

//粉丝列表关注与取消关注
function followfans(userid,authorid,thzs){
	 var that = $(thzs);
     if(that.hasClass('information-noSwitch') || that.hasClass('information-iMutual')){
    	 layer.open({
 	        content: '是否取消关注?',
 	        btn: ['确定', '取消'],
 	        yes: function (index) {
 	        	$.ajax({ 
 	  			 url: '/operate/'+ userid + '/cancel/' + authorid, 
 	  			 type: "POST",
 	  	         data: {},
 	  	         dataType: "json",//请求数据类型
 	  			 success: function(data){
 	  				 if(data.code == 0){
 	  					 layer.close(index);
 	  					 that.removeClass("information-noSwitch");
 	  					 that.removeClass("information-iMutual");
 	  					 //我的关注减1
 	  					 var num = Number($('#userFollow').text());
 	  					 if(num-1 >= 0){
 	  						 $('#userFollow').text(num-1);
 	  					 }else{
 	  						 $('#userFollow').text(0);
 	  					 }
 	  				     //用户粉丝减1
 	  					 var nums = Number($('#' + authorid + 'fans').text());
 	  					 if(nums-1 >= 0){
 	  						$('#' + authorid + 'fans').text(nums-1);
 	  					 }else{
 	  						$('#' + authorid + 'fans').text(0);
 	  					 }
 	  				     that.text("+关注")
 	  				 }else{
 	  					 layer.msg(data.msg,{icon:7});
 	  				 }
 	  			 },
 	  			 error : function() {
 	  				 layer.msg("取消关注异常",{icon:7});
 	  			 }
 	  		 })
 	        },
 	        no: function () {

 	        }
 	    });
     }else{
    	 $.ajax({ 
    		 url: '/operate/'+ userid + '/follow/' + authorid, 
    		 type: "POST",
            data: {},
            dataType: "json",//请求数据类型
    		 success: function(data){
    			 var result = data.data;
    			 if(data.code == 0){
    				if(result == 1){
    					that.addClass("information-noSwitch");
    			        that.text("✓已关注")
    				 }else if(result == 2){
    					that.addClass("information-iMutual");
    				    that.text("⇋互为关注")
    				 }
    				//我的关注加1
    				 var num = Number($('#userFollow').text());
    			     $('#userFollow').text(num+1);
    			     //用户粉丝加1
    			     console.log(authorid + 'fans');
    			     var nums = Number($('#' + authorid + 'fans').text());
	  				 $('#' + authorid + 'fans').text(nums+1);
    			 }else{
    				 layer.msg(data.msg);
    			 }
    		 },
    		 error : function() {
    			 layer.msg("关注失败");
    		 }
    	 })
     }
}
